package com.lcf.app.minio.demo;

import io.minio.BucketExistsArgs;
import io.minio.MakeBucketArgs;
import io.minio.MinioClient;
import io.minio.SetBucketPolicyArgs;
import io.minio.errors.*;
import io.minio.messages.Bucket;

import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.List;

public class BucketDemo {

    static MinioClient minioClient =
            MinioClient.builder()
                    .endpoint("http://192.168.254.18:9000")
                    .credentials("minioadmin", "minioadmin")
                    .build();

    public static void main(String[] args)
            throws IOException, NoSuchAlgorithmException, InvalidKeyException {
        try {
            // 查看桶 ‘my-bucket’ 是否存在
            String bucketName = "my-bucket";
            boolean found =
                    minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build());
            if (found) {
                System.out.println(bucketName + "桶已经存在。");
            } else {
                minioClient.makeBucket(MakeBucketArgs.builder().bucket(bucketName).build());//创建桶
                //minioClient.makeBucket(MakeBucketArgs.builder().bucket(bucketName).region("region").build());
                //minioClient.makeBucket(MakeBucketArgs.builder().bucket(bucketName).objectLock(true).build());//创建桶并使用对象锁
                System.out.println(bucketName + "桶创建成功。");
            }

            // 设置桶策略:或者控制台设置
            //minioClient.setBucketPolicy(SetBucketPolicyArgs.builder().config("").build());

            List<Bucket> buckets = minioClient.listBuckets();
            System.out.print("桶列表：");
            buckets.forEach(v -> System.out.print(v.name() + " , "));
        } catch (MinioException e) {
            System.out.println("ERROR: " + e);
        }
    }

}
